package com.example.index.service

import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.widget.Toast
import com.example.index.IEventBusService
import com.example.index.User
import es.dmoral.toasty.Toasty
import org.greenrobot.eventbus.EventBus

class EventBusServerService : Service() {


    val users = arrayListOf<User>()

    override fun onBind(intent: Intent?) :IBinder {
        EventBus.getDefault().post(BindEvent(true))
       return binder
    }


    private val binder = object : IEventBusService.Stub() {

        override fun postUser(): MutableList<User> {

            return mutableListOf(*users.toTypedArray())
        }

        override fun addUser(user: User): Boolean {
            users.add(user)
            return true
        }


    }

    override fun onRebind(intent: Intent?) {
        Toasty.info(this, "RemoteService Rebind", Toast.LENGTH_SHORT).show()
        super.onRebind(intent)
    }


    override fun onUnbind(intent: Intent?): Boolean {
        EventBus.getDefault().post(BindEvent(true))
        Toasty.info(this, "RemoteService Unbind", Toast.LENGTH_SHORT).show()
        return super.onUnbind(intent)
    }
}
